/**
 * @author jmdelgado
 */
WebKitDev.Utils.Resources = {
    ScriptsLoaded : new WebKitDev.Core.Structures.Dictionary(),
    SetScriptStatus : function(scriptName, status) {
        var sc = ScriptsLoaded.GetValue(scriptName);
        sc.Status = status;
        ScriptsLoaded.SetValue(scriptName, sc);
    },
    LoadScript : function(ScriptUrl) {
        try {
            LoadScriptInitializer(ScriptUrl, null);
        } catch(e) {
            throw e;
        }
    },
    LoadScriptInitializer : function(url, initializer) {
        try {
            var reso = new WebKitDev.Core.Structures.Resource(null, url);
            //Si ya se cargó un recurso, se producirá una excepción a intentar añadirlo:
            //OJO Unicamente si tiene la misma URL
            this.parent.ScriptsLoaded.Add(reso.Name, reso);
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.Name = reso.Name;
            if (script.readyState) {//IE
                script.onreadystatechange = function() {
                    if (script.readyState == "loaded" || script.readyState == "complete") {
                        script.onreadystatechange = null;
                        WebKitDev.Utils.Resources.SetScriptStatus(script.Name, WebKitDev.Core.Enums.ResourceStatus.Loaded);
                        eval(initializer);
                    }
                };
            } else {//Others
                script.onload = function() {
                    WebKitDev.Utils.Resources.SetScriptStatus(script.Name, WebKitDev.Core.Enums.ResourceStatus.Loaded);
                    eval(initializer);
                };
            }
            script.src = url;
            document.getElementsByTagName("head")[0].appendChild(script);
        } catch(err) {
            if (err.message.split(':')[0] != "WebKitDev.Core.Structures.Dictionary.Add")
                throw err;
        }
    }
}
